<?php

class Plugin_Base extends Zend_Controller_Plugin_Abstract {

    /**
     *
     * @var Zend_View
     */
    protected $_view;

    function __construct()
    {
        $this->_view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('layout')->getView();
    }

    public function dispatchLoopStartup(\Zend_Controller_Request_Abstract $request)
    {
        $this->_view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'Helper_');
        $this->_view->addScriptPath(APPLICATION_PATH . '/layouts/scripts');
        
        $this->_addViewHelpers();
        $this->_addLayout($request);
        $this->_addCssFiles();
        $this->_addJsFiles();
    }

    private function _addLayout(\Zend_Controller_Request_Abstract $request)
    {
        if ('default' === $request->getModuleName()) {
            return;
        }
        $pathLayout = APPLICATION_PATH . "/modules/{$request->getModuleName()}/layouts/scripts";
        $fileLayout = $pathLayout . "/layout.phtml";
        if (file_exists($fileLayout)) {
            $layout = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('layout');
            $layout->setLayoutPath($pathLayout);
        }
    }

    protected function _addViewHelpers()
    {
        
    }

    private function _addJsFiles()
    {
        $arrayFiles = array(
            '/jquery/js/jquery-1.8.2.js',
            '/jquery/js/jquery-ui-1.9.1.custom.min.js',
            '/bootstrap/js/bootstrap.min.js',
            '/js/init.js'
        );

        $files = array_merge($arrayFiles, $this->_getArrayJsFiles());

        foreach ($files as $file) {
            $this->_view->headScript()->appendFile($this->_view->baseUrl() . $file);
        }
    }

    private function _addCssFiles()
    {
        $arrayFiles = array(
            '/jquery/css/smoothness/jquery-ui-1.9.1.custom.min.css',
            '/bootstrap/css/bootstrap.css',
            '/css/style.css'
        );

        $files = array_merge($arrayFiles, $this->_getArrayCssFiles());
        foreach ($files as $file) {
            $this->_view->headLink()->appendStylesheet($this->_view->baseUrl() . $file);
        }
    }

    protected function _getArrayJsFiles()
    {
        return array();
    }

    protected function _getArrayCssFiles()
    {
        return array();
    }

}